/**
 * 
 */
package org.chungyan5.lib.android.gcal;

import java.util.Calendar;

import org.chungyan5.lib.android.gcal.CalendarContract.Events;
import org.chungyan5.lib.android.gcal.CalendarContract.Instances;

import android.content.ContentUris;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.util.Log;

/**
 * @author yan
 *
 */
public class SdkVer8Plus extends GoogleCalendarAPI {

	private static final String DEBUG_TAG = "GoogleCalendarAPI";
	private final String PRE_CALENDAR_URL = "content://com.android.calendar/"	// Android 2.2 or above
											//"content://calendar/"				// before Android 2.2 
											//"content://calendarEx/"			// Motorola devices 
											; 
	
	@Override
	public String getTestStr() {//
		testStr += String.valueOf(Build.VERSION.SDK_INT) + " ";
		testStr += String.valueOf(Build.VERSION_CODES.FROYO) + " ";
		
		return testStr;
	}

	@Override
	public boolean read() {
		Cursor cursor = null;
		Uri calendarsUri;
		
		// list calendars =================
		calendarsUri = Uri.parse( PRE_CALENDAR_URL + "calendars");
		cursor = cr.query(calendarsUri, null, null, null, null);
		
		if (cursor!=null && cursor.moveToFirst()) {
			
			Log.i(DEBUG_TAG, "space");
			
			for (int i = 0; i < cursor.getColumnCount(); i++) {
				Log.i(DEBUG_TAG, cursor.getColumnName(i) + "=" + cursor.getString(i));
			}
		} else Log.i(DEBUG_TAG, "cursor as null"); 
		
		// list events =================
		calendarsUri = Uri.parse( PRE_CALENDAR_URL + "calendars");
		
		return true;
	}

	@Override
	public boolean insert() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean update() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean remove() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public GCalEventList listAllEvents(Calendar beginningDate, Calendar endDate, GCal gcal) {

		Uri.Builder builder = Instances.CONTENT_URI.buildUpon();
		
		ContentUris.appendId(builder, beginningDate.getTimeInMillis());
		ContentUris.appendId(builder, endDate.getTimeInMillis()); 
		
		return new GCalEventList( cr.query(
				builder.build(),
				
				new String[]{ Instances.EVENT_ID, Events.TITLE, Instances.BEGIN, Instances.END, Instances.DURATION, Events.ALL_DAY, Events.CALENDAR_ID},
//				new String[]{ "title", "begin", "end", "allDay"},
				
//				Events.CALENDAR_ID + " = ? AND " + Instances.BEGIN + " >=? AND " + Instances.BEGIN + " <?",
//				Instances.CALENDAR_ID + " = ?",
				"Calendars._id=" + gcal.getId(),
				
//				new String[] { gcal.getId(), String.valueOf(beginningDate.getTimeInMillis()), String.valueOf(endDate.getTimeInMillis()) },
				null,
				
				null)
				
				);
		/*return new GCalEventList( cr.query(
					Events.CONTENT_URI,
					
//					null,
//					new String[]{ Events._ID, Events.TITLE, Events.DTSTART, Events.DTEND},
					new String[]{ Events._ID, Events.TITLE, Events.DTSTART, Events.DTEND, Events.CALENDAR_ID},
					
//					null,
//					Events.CALENDAR_ID + " = ?",
					Events.CALENDAR_ID + " = ? AND " + Events.DTSTART + " >=? AND " + Events.DTSTART + " <?",
					
//					null,
//					new String[] { gcal.getId() }, 
					new String[] { gcal.getId(), String.valueOf(beginningDate.getTimeInMillis()), String.valueOf(endDate.getTimeInMillis()) },
					
					null)
				);*/
	}
	
}
